#include "unit.h"
//subunit++++++++++++++++++++++++++++++++++++++++
// the base unit. the stats for a single ship, not
// an actual instance of the ship.  those will be
// represented by an integer in unit which references
// subunit
//-----------------------------------------------
Units::subunit::subunit(int arm, int shd, int eva, int spd){
    armor = arm;
    shield = shd;
    evasion = eva;
    speed = spd;
    for(int i = 0; i < numBats; i++){
        batery[i] = NULL;
    }
}
Units::subunit::subunit(subunit* u){
    armor = u->armor;
    shield = u->shield;
    evasion = u->evasion;
    speed = u->speed;
    for(int i = 0; i < maxBat; i++)
        batery[i] = u->batery[i];
}
Units::subunit::~subunit(){
    for(int i = 0; i < numBats; i++){
        if(batery[i] != NULL){
            delete batery[i];
        }
    }
}
void Units::subunit::addBatery(bateryType btp, int dam, int acc,
        int mnr, int mxr, int ap){
    if(batery[btp] == NULL){
        batery[btp] = new bullet();
    }
    batery[btp]->damage(dam);
    batery[btp]->accuracy(acc);
    batery[btp]->minRange(mnr);
    batery[btp]->maxRange(mxr);
    batery[btp]->apCost(ap);
}
Units::bullet* Units::subunit::shoot(bateryType btp){
    bullet* bt = batery[btp]->shoot();
    return bt;
}
int Units::subunit::dead(int n, voley* vol, bool shd){
    bullet* shot;
    int kia = 0;
    for(int i = 0; i < n; i++){
        shot = vol->nextShot();
        if(shd){
            if((shot->accuracy() - evasion > rand()%100) && (sunitdeath(shot->damage(), armor, shield))){
                kia++;
            }
        } else {
            if((shot->accuracy() - evasion > rand()%100) && (sunitdeath(shot->damage(), armor, 0))){
                kia++;
            }
        }
    }
    return kia;
}
//===============================================